package cn.jinhe.doc.view.action;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.jinhe.doc.base.BaseAction;
import cn.jinhe.doc.domain.Material;
import cn.jinhe.doc.domain.MaterialType;
import cn.jinhe.doc.service.MaterialTypeService;
import cn.jinhe.doc.util.QueryHelper;

@Controller
@Scope("prototype")
public class MaterialAction extends BaseAction<Material>{

	private Long materialTypeId;
	
	public Long getMaterialTypeId() {
		return materialTypeId;
	}
	public void setMaterialTypeId(Long materialTypeId) {
		this.materialTypeId = materialTypeId;
	}
	/*
	 * 列表
	 */
	public String list() throws Exception {
		//List<User> userList = userService.findAll();
		//ActionContext.getContext().put("userList", userList);
		
		//准备分页信息
		new QueryHelper(Material.class, "m").preparePageBean(materialService, pageNum, pageSize);
		
		return "list";
	}
	/*
	 * 删除
	 */
	public String delete() throws Exception {
		materialService.delete(model.getId());
		return "toList";
	}

	/*
	 * 添加
	 */
	public String add() throws Exception {
		
/*		model.setDepartment(departmentService.getById(departmentId));
		List<Role> roleList=roleService.getByIds(roleIds);
		model.setRoles(new HashSet<Role>(roleList));
//		model.setRoles(new ArrayList().addAll(roleList));
		
		String md5Digest=DigestUtils.md5Hex("1234");
		model.setPassword(md5Digest);*/
		
		
		
		model.setStore((long) 0);
		model.setMaterialType(materialTypeService.getById(materialTypeId));
		materialService.save(model);

		return "toList";
	}

	/*
	 * 添加页面
	 */
	public String addUI() throws Exception {
	
		List<MaterialType> materialTypeList=materialTypeService.findAll();
		ActionContext.getContext().put("materialTypeList", materialTypeList);
		
		
		List<Material> materialList=materialService.findAll();
		
		ActionContext.getContext().put("materialList", materialList);
		return "saveUI";	
	}

	/*
	 * 修改页面
	 */
	public String editUI() throws Exception {
		
		List<MaterialType> materialTypeList=materialTypeService.findAll();
		ActionContext.getContext().put("materialTypeList", materialTypeList);
		
		
		Material material=materialService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(material);

		return "saveUI";
	}

	/*
	 * 修改
	 */
	public String edit() throws Exception {
		//1,从数据库中取出原对象
		Material material=materialService.getById(model.getId());
		
		//2，设置要修改的属性
		material.setName(model.getName());
		material.setSerialNumber(model.getSerialNumber());;
		material.setSpecification(model.getSpecification());
		material.setUnit(model.getUnit());
		material.setStore(model.getStore());
		material.setMaterialType(materialTypeService.getById(materialTypeId));
		materialService.update(material);
		
		return "toList";
	}
}
